#!/bin/bash
#使用华为云的镜像
#不能以root用户启动，默认创建用户elasticsearch

version="7.6.1"
cd $HOME

# 指定ES地址
read -p "Enter es host and port,default:http://127.0.0.1:9200 " esAddress
if [ -z "$esAddress" ]; then
	esAddress="http://127.0.0.1:9200"
fi
echo "your es host and port is: $esAddress"

# 端口
read -p "Enter kibana port,default:5601 " kibanaPort
if [ -z "$kibanaPort" ]; then
	kibanaPort="5601"
fi
echo "your kibana port is: $kibanaPort"

mkdir -p /data/tools
cd /data/tools

package_url="https://mirrors.huaweicloud.com/kibana/${version}/kibana-${version}-linux-x86_64.tar.gz"

wget -c ${package_url} || exit 1

tar zxvf kibana-${version}-linux-x86_64.tar.gz

ln -sf kibana-${version}-linux-x86_64 kibana
mkdir -p /data/tools/kibana/logs

cd /data/tools/kibana


sudo tee /data/tools/kibana/config/kibana.yml <<-'EOF'
server.port: ###kibanaPort###
server.host: "0.0.0.0"
kibana.index: ".kibana"
elasticsearch.hosts: "###esAddress###"
i18n.locale: "zh-CN"
EOF

sed -i 's/###kibanaPort###/'$kibanaPort'/g'  /data/tools/kibana/config/kibana.yml
sed -i 's,###esAddress###,'$esAddress',g' /data/tools/kibana/config/kibana.yml

chown -v elasticsearch:elasticsearch /data/tools/kibana /data/tools/kibana-${version}-linux-x86_64 -R


echo "Add user elasticsearch! can not start by root!"
useradd elasticsearch


# echo 'Startting kibana'
# su elasticsearch
# nohup /data/tools/kibana/bin/kibana >> /data/tools/kibana/logs/kibana.log 2>&1 &

echo 'note:kibana install success! Start kibana by executing the following command'
echo 'su elasticsearch'
echo 'nohup /data/tools/kibana/bin/kibana >> /data/tools/kibana/logs/kibana.log >/dev/null 2>&1 &'


